Vue.prototype.$http=axios
axios.defaults.baseURL="http://127.0.0.1:8081"


//解析url参数函数
function parseParamObj(url) {
    let paramsStr = url.substring(url.indexOf("?")+1); //typeId=27
    let paramsArray = paramsStr.split("&"); //typeId=27 xxx=xxx
    let paramObj = {};
    $.each(paramsArray,function(index,param){
        let parmName = param.split("=")[0];
        let parmValue = param.split("=")[1];
        paramObj[parmName] = parmValue;
    })
    return paramObj;
}


function generateUUID() {
    let dt = new Date().getTime();
    const uuid = 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) {
        const r = (dt + Math.random() * 16) % 16 | 0;
        dt = Math.floor(dt / 16);
        return (c === 'x' ? r : (r & 0x3 | 0x8)).toString(16);
    });
    return uuid;
}



/*
  拦截器:
    axios 的请求拦截器
      在里面做配置,在请求头里面携带token

    axios 的响应拦截器
      在里面做配置,如果登录过期,跳转到登录页面

 */
axios.interceptors.request.use(config=>{
    //console.debug(config)
    //1.获取localStroage里面的token
    let token = localStorage.getItem("token");
    //2.判断token 如果不为long 把token加在请求头里面
    if (token){
        config.headers.TOKEN=token;
    }
    return config;
},error => {
    return Promise.reject(error);
})


axios.interceptors.response.use(resp=>{
    // console.log("响应拦截器执行:",resp)
    let {success,msg}=resp.data
    if (!success && msg === "noLogin"){
       location.href="index.html"
    }
    if (!success && msg === "noPermission"){
        location.href="index.html"
    }
    return resp;
},error => {
    return Promise.reject(error);
})